package global

import (
	"gin-wheel/pkg/setting"
	"os"
	"strings"

	ut "github.com/go-playground/universal-translator"
	"github.com/gomodule/redigo/redis"
	"go.uber.org/zap"
	"gorm.io/gorm"
)

// 项目中使用到的全局变量
var (
	BasePath string // 定义项目的根目录

	ServerSetting    setting.HttpServerSetting
	JWTSetting       setting.JWTSetting
	LogsSetting      setting.LogsSetting
	HashIdsSetting   setting.HashIdsSetting
	RedisSetting     setting.RedisSetting
	SnowFlakeSetting setting.SnowFlakeSetting

	RedisPool     *redis.Pool
	MysqlConnMaps map[string]*gorm.DB
	ZapLog        *zap.Logger
	TransLator    ut.Translator // 翻译器全局变量

	// rabbit相关
	RabbitHelloSetting     setting.RabbitHelloSetting
	RabbitPubsubSetting    setting.RabbitPubsubSetting
	RabbitWorkQueueSetting setting.RabbitWorkQueueSetting
	RabbitRouteSetting     setting.RabbitRoutSeting
	RabbitTopicSetting     setting.RabbitTopicSetting
)

func init() {
	// 1.初始化程序根目录
	if curPath, err := os.Getwd(); err == nil {
		// 路径进行处理，兼容单元测试程序程序启动时的奇怪路径
		if len(os.Args) > 1 && strings.HasPrefix(os.Args[1], "-test") {
			BasePath = strings.Replace(strings.Replace(curPath, `\test`, "", 1), `/test`, "", 1)
		} else {
			BasePath = curPath
		}
	} else {
		panic(err)
	}
}
